﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using PhotoFramePublishingPlugin.Properties;

namespace PhotoFramePublishingPlugin
{
    internal partial class ConfigurationSettingsForm : Form, IConfigurationSettingsView
    {
        private readonly ConfigurationSettingsPresenter _presenter;

        public ConfigurationSettingsForm(IConfigurationSettingsModel model)
        {
            InitializeComponent();

            _presenter = new ConfigurationSettingsPresenter(this, model);

            _destinationTextBox.DataBindings.Add("Text", _presenter, "DestinationRoot", false, DataSourceUpdateMode.OnPropertyChanged);

            _maximumSizeComboBox.FormattingEnabled = true;
            _maximumSizeComboBox.Format += FormatImageSize;

            _maximumSizeComboBox.DataSource = _presenter.MaximumSizeOptions;
            _maximumSizeComboBox.DataBindings.Add("SelectedItem", _presenter, "ImageSize");

            addBordersCheckBox.DataBindings.Add("Checked", _presenter, "ShouldAddBorders", false, DataSourceUpdateMode.OnPropertyChanged);
            shortenFileNamesCheckBox.DataBindings.Add("Checked", _presenter, "ShouldShortenFileNames", false, DataSourceUpdateMode.OnPropertyChanged);

            Load += _presenter.OnLoad;

            // WPF would have us use an ICommand here...
            okButton.Click += _presenter.OnClickOkButton;
        }

        private static void FormatImageSize(object sender, ListControlConvertEventArgs e)
        {
            ImageSize size = (ImageSize) e.ListItem;
            if (size.IsValid)
            {
                e.Value = string.Format("{0}: {1} x {2}", size.Description, size.Width, size.Height);
            }
            else
            {
                e.Value = size.Description;
            }
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            // TODO: The button ought to be bound to the presenter, which should ask the view for the dialog factory, and then it deals with it?
            if (_presenter.BrowseForFolder(new FolderBrowserDialogFactory()))
                _destinationTextBox.Text = _presenter.DestinationRoot;
        }

        private void productLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(productLink.Text);
        }

        public bool OkButtonEnabled
        {
            get { return okButton.Enabled; }
            set { okButton.Enabled = value; }
        }

        public void SetBorderPreview(BorderPreviewMode borderPreviewMode)
        {
            switch (borderPreviewMode)
            {
                case BorderPreviewMode.Cropped:
                    borderPreviewPictureBox.Image = Resources.PortraitCropped;
                    break;
                case BorderPreviewMode.WithBorders:
                    borderPreviewPictureBox.Image = Resources.PortraitWithBorders;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("borderPreviewMode");
            }
        }

        public void SetVersionLabel(string version)
        {
            versionLabel.Text = version;
        }

        public void SetDestinationError(string errorMessage)
        {
            errorProvider.SetError(_destinationTextBox, errorMessage);
        }

        public void ClearDestinationError()
        {
            errorProvider.SetError(_destinationTextBox, null);
        }
    }
}
